package com.zq.widget.inverted.game.role.equipment.weapon.gun.bullet;

import android.content.Context;
import android.graphics.PointF;
import android.graphics.RectF;

import com.zq.widget.inverted.R;
import com.zq.widget.inverted.game.role.Role;
import com.zq.widget.inverted.game.role.RoleGroup;
import com.zq.widget.inverted.game.role.damage.DamageProxy;
import com.zq.widget.inverted.game.role.equipment.weapon.gun.BulletFactory;
import com.zq.widget.inverted.game.role.equipment.weapon.gun.Gun;
import com.zq.widget.inverted.game.role.equipment.weapon.gun.Gun2;
import com.zq.widget.inverted.game.role.equipment.weapon.missile.Missile;
import com.zq.widget.inverted.game.role.hp.HPProxy;

/**
 * Created by zhangqiang on 2018/4/3.
 */
public class BulletFactoryImpl implements BulletFactory {

    private static final int minBulletCount = 2;
    private static final int maxBulletCount = 5;
    private static final int minBulletVelocity = 800;
    private static final int maxBulletVelocity = 1200;
    private static final int minDamage = 1;
    private static final int maxDamage = 5;
    private Context context;
    private int drawableResId;
    private int degree;

    public BulletFactoryImpl(Context context, int drawableResId, int degree) {
        this.context = context;
        this.drawableResId = drawableResId;
        this.degree = degree;
    }


    @Override
    public Role[] create(Gun gun) {

        float factor = gun.levelFactor();
        int count = computeCount(factor);
        long velocity = computeVelocity(factor);
        PointF pointF = gun.shootPoint();

        Role[] roles = new Role[count];
        for (int i = 0; i < count; i++) {

            Bullet bullet = new Bullet(context, drawableResId);
            float width = bullet.bounds().width();
            float x = pointF.x - width * count / 2 + width * i;
            bullet.setDamageProxy(new DamageProxy(computeDamage(factor)));
            bullet.setHpProxy(new HPProxy(1));
            bullet.setStartPoint(new PointF(x, pointF.y));
            bullet.setDegree(degree);
            bullet.setVelocity(velocity);
            roles[i] = bullet;
        }
        for (Role role : roles) {
            role.locationOffset(0, -role.bounds().height() / 2 - 10);
        }
        return roles;
    }

    private int computeCount(float factor) {

        return (int) (factor * factor * (maxBulletCount - minBulletCount) + minBulletCount);
    }

    private long computeVelocity(float factor) {

        return (long) (factor * (maxBulletVelocity - minBulletVelocity) + minBulletVelocity);
    }

    private long computeDamage(float factor) {

        return (long) (factor * (maxDamage - minDamage) + minDamage);
    }
}
